package be.kuleuven.mume.arrayAdapters;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.Update;
import be.kuleuven.mume.hulpklassen.DrawableManager;

public class UpdatesArrayAdapter extends ArrayAdapter<Update>{
	
	private static final String TAG = "UpdatesArrAdpt";
	private static final int textViewResourceId = R.layout.rij_update;
	private DrawableManager dm = new DrawableManager();
	
	public UpdatesArrayAdapter(Context context, List<Update> objects) {
		super(context, textViewResourceId, objects);
	}
	
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		if (view == null ){//|| this.getItemViewType(position) != textViewResourceId){
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(textViewResourceId, null);
			Log.d(TAG, "inflated updats view");
		}
	        
        Update u = this.getItem(position);
        if (u != null) {
        	TextView text = (TextView) view.findViewById(R.id.updatetext);
        	if (text == null)
            	Log.e(TAG, "textview updateText not found");	
            text.setText(u.getText());
            
            text = (TextView) view.findViewById(R.id.author);
            if (text == null)
            	Log.e(TAG, "textview author not found");
            text.setText(u.getAuthor());
            
            ImageView icon = (ImageView) view.findViewById(R.id.icon);
            if(icon != null && u.getImage() != null)
            	dm.fetchDrawableOnThread(u.getImage(), icon);
        } else
        	Log.e(TAG, "Update on position " + position + "not found!");
        return view;
	}
}
